package com.xxkj.nmip.page;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.xxkj.nmip.R;
import com.xxkj.nmip.dialog.UpDateDialog;

import java.util.ArrayList;
import java.util.List;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class WelComeActivity extends BaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
    protected ImageView iv_bg;
    protected Activity activity;
    private boolean hasCountTime = false;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_welcome);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        activity = this;
        iv_bg = findViewById(R.id.iv_bg);
        Glide.with(this).asGif().load(R.mipmap.loading)
                .fitCenter().into(iv_bg);
        iv_bg.postDelayed(new Runnable() {
            @Override
            public void run() {
                hasCountTime = true;
            }
        },3000L);
        WelComeActivityPermissionsDispatcher.goNextWithPermissionCheck(this);
    }
    @NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA})
    public void goNext() {
        if (hasCountTime) gotoMainActivity();
        else {
            iv_bg.postDelayed(new Runnable() {
                @Override
                public void run() {
                    gotoMainActivity();
                }
            },3000L);
        }
    }

    @OnShowRationale({Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA})
    public void showRationaleForAllPermission(final PermissionRequest request) {
        request.proceed();
        /*AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setPositiveButton("同意", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setCancelable(false)
                .setMessage("护理管理想要访问您的存储、电话、相机、麦克风权限").create();
        alertDialog.setCancelable(false);
        alertDialog.show();*/

    }
    @OnNeverAskAgain({Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA})
    public void onNeverAskAllPermission() {
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setTitle("提示")
                .setIcon(R.mipmap.logo)
                .setMessage
                ("以下权限是系统运行必需，请前往应用设置页授予权限。\n" +
                        "存储、电话、相机、麦克风").setPositiveButton("前往授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //去到设置页面
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }
        }).setNegativeButton("退出系统", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        }).create();
        alertDialog.setCancelable(false);
        alertDialog.show();
    }
    @OnPermissionDenied({Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,Manifest.permission.CAMERA})
    public void onAllPermissionDenied() {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setMessage
                ("以下权限是系统运行必需，请前往应用设置页授予权限。\n" +
                        "存储、电话、相机、麦克风").setPositiveButton("前往授权", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //去到设置页面
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }
        }).setNegativeButton("退出系统", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        }).create();
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        WelComeActivityPermissionsDispatcher.goNextWithPermissionCheck(this);
    }

    /*@Override
    protected void onResume() {
        super.onResume();
        WelComeActivityPermissionsDispatcher.goNextWithPermissionCheck(this);
    }*/
    private void gotoMainActivity() {

        Intent intent = new Intent(WelComeActivity.this, MainActivity.class);
        startActivity(intent);
        WelComeActivity.this.finish();
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        WelComeActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
